package io.renren.modules.sys.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.CustomerDao;
import io.renren.modules.sys.entity.CustomerEntity;
import io.renren.modules.sys.service.CustomerService;


@Service("customerService")
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity> implements CustomerService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	String name = (String)params.get("name");
		String phone = (String)params.get("phone");
		String tel = (String)params.get("tel");
		String url = (String)params.get("url");
		String address = (String)params.get("address");
		Integer isMake=null;
		if(null!=params.get("isMake")&&!StringUtils.isEmpty(params.get("isMake").toString())) {
			isMake = Integer.valueOf(params.get("isMake").toString());
		}
		Integer type=null;
		if(null!=params.get("type")&&!StringUtils.isEmpty(params.get("type").toString())) {
			type = Integer.valueOf(params.get("type").toString());
		}
		Integer followDate=null;
		if(null!=params.get("followDate")&&!StringUtils.isEmpty(params.get("followDate").toString())) {
			 followDate=Integer.valueOf(params.get("followDate").toString());
		}
	    Date tomorrow=null;
	    if(null!=followDate) {
	        Date today = new Date();
	        Calendar c = Calendar.getInstance();
	        c.setTime(today);
	        c.add(Calendar.DAY_OF_MONTH, -followDate);// 今天+1天
	        tomorrow = c.getTime();
	    }
	    
	    Integer followDate2=null;
		if(null!=params.get("followDate2")&&!StringUtils.isEmpty(params.get("followDate2").toString())) {
			 followDate=Integer.valueOf(params.get("followDate2").toString());
		}
	    Date tomorrow2=null;
	    Date tomorrow3=null;
	    if(null!=followDate2) {
	        Date today = new Date();
	        Calendar c = Calendar.getInstance();
	        c.setTime(today);
	        c.add(Calendar.DAY_OF_MONTH, -55);// 今天+1天  55
	        tomorrow2 = c.getTime();
	        Date today1 = new Date();
	        Calendar c1 = Calendar.getInstance();
	        c1.setTime(today);
	        c1.add(Calendar.DAY_OF_MONTH, -60);// 今天+1天  55
	        tomorrow3=c1.getTime();
	    }
	    
    	Integer userId=null;
    	if(null!=params.get("userId")&&!StringUtils.isEmpty(params.get("userId").toString())) {
    		userId=Integer.valueOf(params.get("userId").toString());
    	}
    	Integer customerStatus=null;
    	if(null!=params.get("customerStatus")&&!StringUtils.isEmpty(params.get("customerStatus").toString())) {
    		customerStatus=Integer.valueOf(params.get("customerStatus").toString());
    	}
    	String email = (String)params.get("email");
    	String city = (String)params.get("city");
        Page<CustomerEntity> page = this.selectPage(
                new Query<CustomerEntity>(params).getPage(),
                new EntityWrapper<CustomerEntity>().
                like(StringUtils.isNotBlank(name),"name", name)
                .addFilter("status={0}", 1)
                .addFilterIfNeed(StringUtils.isNotBlank(phone),"phone={0}", phone)
                .addFilterIfNeed(StringUtils.isNotBlank(email),"email={0}", email)
                .addFilterIfNeed(StringUtils.isNotBlank(city),"city={0}", city)
                .addFilterIfNeed(StringUtils.isNotBlank(tel),"tel={0}", tel)
                .addFilterIfNeed(StringUtils.isNotBlank(url),"url={0}", url)
                .addFilterIfNeed(StringUtils.isNotBlank(address),"address={0}", address)
                .addFilterIfNeed(null!=type,"type={0}", type)
                .addFilterIfNeed(null!=userId,"user_id={0}", userId)
                .addFilterIfNeed(null!=customerStatus,"customer_status={0}", customerStatus)
                .addFilterIfNeed(null!=isMake,"is_make={0}", isMake)
				.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
				.le(null==followDate?false:true, "upd_date", tomorrow)
				.le(null!=followDate2, "upd_date",tomorrow2)
				.gt(null!=followDate2, "upd_date",tomorrow3)
				.orderBy("upd_date", false)
        );
        for (CustomerEntity customerEntity : page.getRecords()) {
        	customerEntity.setDays(days(new Date(), customerEntity.getUpdDate()));
		}
        return new PageUtils(page);
    }
    
    @Override
    public PageUtils dellist(Map<String, Object> params) {
    	String email = (String)params.get("email");
    	String city = (String)params.get("city");
    	String name = (String)params.get("name");
		String phone = (String)params.get("phone");
		String tel = (String)params.get("tel");
		String url = (String)params.get("url");
		String address = (String)params.get("address");
		if(null!=name) {
			try {
				name= URLDecoder.decode(url, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}  
		}
		Integer type=null;
		if(null!=params.get("type")&&!StringUtils.isEmpty(params.get("type").toString())) {
			type = Integer.valueOf(params.get("type").toString());
		}
		Integer followDate=null;
		if(null!=params.get("followDate")&&!StringUtils.isEmpty(params.get("followDate").toString())) {
			 followDate=Integer.valueOf(params.get("followDate").toString());
		}
	    Date tomorrow=null;
	    if(null!=followDate) {
	        Date today = new Date();
	        Calendar c = Calendar.getInstance();
	        c.setTime(today);
	        c.add(Calendar.DAY_OF_MONTH, -followDate);// 今天+1天
	        tomorrow = c.getTime();
	    }
    	Integer userId=null;
    	if(null!=params.get("userId")&&!StringUtils.isEmpty(params.get("userId").toString())) {
    		userId=Integer.valueOf(params.get("userId").toString());
    	}
    	Integer customerStatus=null;
    	if(null!=params.get("customerStatus")&&!StringUtils.isEmpty(params.get("customerStatus").toString())) {
    		customerStatus=Integer.valueOf(params.get("customerStatus").toString());
    	}

        Page<CustomerEntity> page = this.selectPage(
                new Query<CustomerEntity>(params).getPage(),
                new EntityWrapper<CustomerEntity>().like(StringUtils.isNotBlank(name),"name", name)
                .addFilter("status={0}", 0)
                .addFilterIfNeed(StringUtils.isNotBlank(phone),"phone={0}", phone)
                .addFilterIfNeed(StringUtils.isNotBlank(email),"email={0}", email)
                .addFilterIfNeed(StringUtils.isNotBlank(city),"city={0}", city)
                .addFilterIfNeed(StringUtils.isNotBlank(tel),"tel={0}", tel)
                .addFilterIfNeed(StringUtils.isNotBlank(url),"url={0}", url)
                .addFilterIfNeed(StringUtils.isNotBlank(address),"address={0}", address)
                .addFilterIfNeed(null!=type,"type", type)
                .addFilterIfNeed(null!=userId,"user_id", userId)
                .addFilterIfNeed(null!=customerStatus,"customer_status={0}", customerStatus)
				.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
				.lt(null==followDate?false:true, "upd_date", tomorrow)
        );
        for (CustomerEntity customerEntity : page.getRecords()) {
        	customerEntity.setDays(days(new Date(), customerEntity.getUpdDate()));
		}
        return new PageUtils(page);
    }
    

    public static int days(Date date1,Date date2)
    {
        int days = (int) ((date1.getTime() - date2.getTime()) / (1000*3600*24));
        return days;
    }


}
